package org.jeecg.modules.monitoring.j_monitoring.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xkcoding.http.util.StringUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.monitoring.j_monitoring.entity.MCabinet;
import org.jeecg.modules.monitoring.j_monitoring.entity.MGlRegulator;
import org.jeecg.modules.monitoring.j_monitoring.entity.MGlStorageResources;
import org.jeecg.modules.monitoring.j_monitoring.entity.MMachineRoom;
import org.jeecg.modules.monitoring.j_monitoring.entity.format.MountCabinetParam;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MCabinetMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MGlRegulatorMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MGlStorageResourcesMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MMachineRoomMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.IMGlRegulatorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: m_gl_drive_cabinet
 * @Author: xiaoyq
 * @Date:   2023-05-25
 * @Version: V1.0
 */
@Service
public class MGlRegulatorServiceImpl extends ServiceImpl<MGlRegulatorMapper, MGlRegulator> implements IMGlRegulatorService {

    @Resource
    private MGlStorageResourcesMapper storageResourcesMapper;
    @Resource
    private MCabinetMapper cabinetMapper;
    @Resource
    private MCabinetServiceImpl cabinetService;
    @Resource
    private MMachineRoomServiceImpl roomService;
    @Resource
    private MMachineRoomMapper mMachineRoomMapper;


    @Override
    public Result mountCabinet(MountCabinetParam param){

        MGlRegulator cabinet = baseMapper.selectById(param.getCabinetId());
        if (cabinet==null){
            return Result.error("机柜错误");
        }

        if (!StringUtil.isEmpty(cabinet.getMountId()) && !cabinet.getMountId().equals(param.getStorageId()) ){
            return Result.error("机柜已绑定其他存储设备");
        }

        MGlStorageResources storageResources =  storageResourcesMapper.selectById(param.getStorageId());
        if (storageResources==null){
            return Result.error("存储资源错误");
        }

        return Result.ok();
    }

    @Override
    public void test(){

        QueryWrapper<MGlRegulator> queryWrapper = QueryGenerator.initQueryWrapper(new MGlRegulator(), new HashMap<>());

        List<MGlRegulator> list = baseMapper.selectList(queryWrapper);
        for (MGlRegulator data : list){
            if (StringUtil.isEmpty(data.getCabinetId())){
                List<MCabinet> cabinets = cabinetMapper.selectByName(data.getCabinet());
                if (cabinets.size()>0){
                    data.setCabinetId(cabinets.get(0).getId());
                    baseMapper.updateById(data);
                }
            }
        }
    }


    public boolean saveBatch(Collection<MGlRegulator> entityList, int size){

        //机柜列表
        List<MCabinet> cabinetList = cabinetMapper.selectAll();

        //机房列表
        List<MMachineRoom> roomList = mMachineRoomMapper.selectAll();

        //处理机房机柜id绑定问题
        for (MGlRegulator entity : entityList){
            entity.setCabinetId(cabinetService.getId(entity.getCabinet(),cabinetList));
            entity.setMachineRoomId(roomService.getId(entity.getMachineRoom(),roomList));
        }


        String sqlStatement = this.getSqlStatement(SqlMethod.INSERT_ONE);
        return this.executeBatch(entityList, size, (sqlSession, entity) -> {
            sqlSession.insert(sqlStatement, entity);
        });

    }
}
